<?php
namespace Admin\Controller;
use Think\Controller;
class ShopController extends CommonController {

    private $shop;

    public function _initialize() {
        $this->shop = D('Shop');
    }

    //店铺列表
    public function index() {
        $state = -2;
        //搜索条件
        $map = array();
        $btn = I('get.btn');
        if ( !empty($btn) ) {
            $state = I('get.state');
            if ($state != -2) $map['state'] = $state;
            $shop_name = trim(I('get.shop_name'));
            if ( !empty($shop_name) ) $map['shop_name'] = array('like',"%$shop_name%");
        }
        //分页和查询数据
        $total = $this->shop->where($map)->count();
        $page = set_page($total, C('PAGE_NUM'));
        $shop = $this->shop->where($map)->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('shop', $shop);
        $this->assign('shop_name', $shop_name);
        $this->assign('state', $state);
        $this->display();
    }


    //已通过店铺
    public function pass() {
        $this->showList(1);
    }

    //不通过店铺
    public function noPass() {
        $this->showList(-1);
    }

    //待审核店铺
    public function check() {
        $this->showList(0);
    }

    //可以传递不同的状态的店铺列表
    private function showList($state) {
        //搜索条件
        $map = array();
        $btn = I('get.btn');
        $map['state'] = $state;
        if ( !empty($btn) ) {
            $shop_name = trim(I('get.shop_name'));
            if ( !empty($shop_name) ) $map['shop_name'] = array('like',"%$shop_name%");
        }
        //分页和查询数据
        $total = $this->shop->where($map)->count();
        $page = set_page($total, C('PAGE_NUM'));
        $shop = $this->shop->where($map)->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('shop', $shop);
        $this->assign('shop_name', $shop_name);
        $this->display();
    }


    //添加店铺
    public function createShop() {
        $this->shop = D('Shop');
        if (IS_POST) {
            //用户账号【也就是手机
            $phone = to_int(I("post.phone"));
            if (!isMobile($phone)) {
                $this->error('请填写正确的手机号码!');
            }
            //验证重复性
            $ret = M('manager')->where(array('mobile'=>$phone))->find();
            if (!empty($res)||!empty($ret)) {
                $this->error("已有该手机号进行注册过");
            }
            $shop_name = I("post.shop_name");
            if (empty($shop_name)) {
                $this->error("店铺名称必需填写！");
            }
            $company = I("post.company");
            if (empty($company)) {
                $this->error("公司名称必需填写！");
            }
            $tel = trim(I("post.tel"));
            if (!islandline($tel)) {
                $this->error("固定电话格式不正确！");
            }
            $address = I("post.address");
            if (empty($address)) {
                $this->error("店铺地址必需填写！");
            }
            $bank_no = trim(I("post.bank_no"));
            if (empty($bank_no)&&is_numeric($bank_no)) {
                $this->error("请正确填写银行卡号");
            }
            $bank_user_name = I("post.bank_user_name");
            if (empty($bank_user_name)) {
                $this->error("银行卡号所有人必需填写！");
            }
            $license_img = I("post.license_img");
            if (empty($license_img)) {
                $this->error("营业执照必需上传！");
            }
            $id_img = I('post.id_img');
            if(empty($id_img)) {
                $this->error("店主身份证必需上传！");
            }
            $create_desc = I("post.create_desc");
            if (empty($create_desc)) {
                $this->error("开店说明必需填写！");
            }
            $shop_name = I("post.shop_name");
            if (empty($shop_name)) {
                $this->error("店铺名称必需填写！");
            }
            $data_one = array(
                'shop_name' => I('post.shop_name'),
                'company' => I('post.company'),
                'tel' => I('post.tel'),
                'address' => I('post.address'),
                'bank_no' => I('post.bank_no'),
                'bank_user_name' => I('post.bank_user_name'),
                'create_desc' => I('post.create_desc'),
                'state' => 1,
                'license_img' => I('post.license_img'),
                'id_img' => $id_img,
                'create_time' => time()
            );


            $bool1 = $this->shop->add($data_one);
            $shop_id = M('shop')->getLastInsId();
            $keeper = I("post.keeper");
            if (!$keeper) {
                $this->error("店主必需填写！");
            }

            $salt = substr(md5(time() . $phone), -8);
            $password = md5(md5(I('post.password')) . $salt);
            $data_two = array(
                'shop_id' => $shop_id,
                'name' => $keeper,
                'mobile' => $phone,
                'salt' => $salt,
                'password' => $password
            );
            $bool2 = M('manager')->add($data_two);
            //店主给默认的权限
            M('role_manager')->add( array('role_id'=>1, 'manager_id'=>$bool2) );
            if ($bool1 && $bool2 == true) {
               $this->success('新增成功', U('Admin/shop/index'));
            } else {
                $this->error('提交失败!');
            }
        } else {
            $this->display();
        }

    }

    //冻结店铺
    public function frozen() {
        $data = array(
            'frozen' => 1
        );
        $bool = M('shop')->where( array('id'=>I('post.shop_id')) )->save($data);
        $this->ajaxReturn($bool);
    }

    //解冻店铺
    public function unfrozen() {
        $data = array(
            'frozen' => 0
        );
        $bool = M('shop')->where( array('id'=>I('post.shop_id')) )->save($data);
        $this->ajaxReturn($bool);
    }



    //店铺信息
    public function getShop() {
        if (IS_POST) {

            $id = I('post.id');
            $shop = $this->shop->where( array('id'=>$id) )->find();
            //如果填写密码则修改，为空就还是原来的密码
            $data_one = array(
                'shop_name' => I('post.shop_name'),
                'company' => I('post.company'),
                'tel' => I('post.tel'),
                'address' => I('post.address'),
                'bank_no' => I('post.bank_no'),
                'bank_user_name' => I('post.bank_user_name'),
                'create_desc' => I('post.create_desc'),
                'state' => I('post.state'),
                'license_img' => I('post.license_img'),
                'id_img' => I('post.id_img'),
                'create_time' => time()
            );


            $info = M('manager')->where(array('shop_id'=>$id))->find();
            $new_password = md5(md5(I('post.password')) . $info['salt']);
            $abc = I("post.password");
            $password = empty(I("post.password")) ? $info['password'] : $new_password;
            $keeper = I("post.keeper");
            if (!$keeper) {
                $this->error("店主必需填写！", $_SERVER['HTTP_REFERER'], 1);
            }
            $phone = to_int(I("post.phone"));
            if (!isMobile($phone)) {
                $this->error('请填写正确的手机号码!', $_SERVER['HTTP_REFERER'], 1);
            }
            $data_two = array(
                'name' => $keeper,
                'mobile' => $phone,
                'password' => $password
            );



            M('manager')->where(array('shop_id'=>$id))->save($data_two);
/*            if ($this->shop->create()) {
                //unlink($shop['license_img']);
                $bool_one = $this->shop->where( array('id'=>$id) )->save($data_one);
                $bool_one? $this->success('修改成功', U('Shop/index')) : $this->error('修改失败!');
            } else {
                $error_info = $this->shop->getError();
                $this->error($error_info);
            }*/

            $bool_one = $this->shop->where( array('id'=>$id) )->save($data_one);
            $bool_one? $this->success('修改成功', U('Shop/index')) : $this->error('修改失败!');

        } else {
            $id = I('get.id');
            $shop = $this->shop->where( array('id'=>$id) )->find();
            //print_r($shop);
            $info = M('manager')->where(array('shop_id'=>$id))->find();
            //echo M('manager')->getLastSql();
            //print_r($info);
            $this->assign('shop',$shop);
            $this->assign('info',$info);
            $this->assign('id',$id);
            $this->display();
        }
    }


}